package com.dxy.df.modules.application;

import com.dxy.df.constants.ErrorEnum;
import com.dxy.df.core.exception.BizException;
import com.dxy.df.core.shiro.ShiroUtils;
import com.dxy.df.modules.bean.entity.UserPublishAcceptRiceEntity;
import com.dxy.df.modules.bean.put.UserAcceptRiceRecordPut;
import com.dxy.df.modules.bean.put.UserPublishAcceptRicePut;
import com.dxy.df.modules.bean.put.UserPublishGiveRicePut;
import com.dxy.df.modules.bean.query.UserPublishAcceptRiceQuery;
import com.dxy.df.modules.bean.vo.UserPublishGiveRiceVo;
import com.dxy.df.modules.service.UserAcceptRiceRecordService;
import com.dxy.df.modules.service.UserPublishAcceptRiceService;
import com.dxy.df.modules.service.UserPublishGiveRiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author wangsheng@dxy.cn
 * @date 2019-10-25
 **/
@Component
public class AcceptRiceApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(AcceptRiceApplication.class);

    @Autowired
    UserPublishAcceptRiceService userPublishAcceptRiceService;
    @Autowired
    UserPublishGiveRiceService userPublishGiveRiceService;
    @Autowired
    UserAcceptRiceRecordService userAcceptRiceRecordService;


    @Transactional(rollbackFor = Exception.class)
    public void publishAcceptRiceSave(UserPublishAcceptRicePut publishAcceptRicePut) {
        //检查是否有收过饭˚
        UserPublishAcceptRiceQuery acceptRiceQuery = new UserPublishAcceptRiceQuery();
        acceptRiceQuery.setUserId(ShiroUtils.getUserAccount());
        acceptRiceQuery.setCreateTime(new Date());
        UserPublishAcceptRiceEntity userPublishAcceptRiceEntity
                = userPublishAcceptRiceService.queryObject(acceptRiceQuery);
        if (null != userPublishAcceptRiceEntity) {
            throw new BizException(ErrorEnum.HAD_ACCEPT_RICE_ASK);
        }

        UserPublishAcceptRiceEntity acceptRiceEntity
                = userPublishAcceptRiceService.save(publishAcceptRicePut);
        //发布收饭的时候，检查是否有匹配
        if (this.accessMatchGiven(ShiroUtils.getUserAccount(),
                acceptRiceEntity.getId(), publishAcceptRicePut.getConditions())) {
            LOGGER.info("-------------start 发布收饭，被自动匹配上----");
            UserPublishAcceptRicePut acceptRicePut = new UserPublishAcceptRicePut();
            acceptRicePut.setId(acceptRiceEntity.getId());
            acceptRicePut.setBizStatus(1);
            userPublishAcceptRiceService.update(acceptRicePut);

        }
    }


    /**
     * 新的收饭是否匹配已有的出饭
     *
     * @param userAccount
     * @param acceptRiceId
     * @return
     */
    public boolean accessMatchGiven(String userAccount, Integer acceptRiceId, String conditions) {

        //收饭信息，包含收的什么饭:conditions
        //查询匹配的出饭信息
        LOGGER.info("查询最早匹配的【出】饭信息....");
        UserPublishGiveRiceVo userPublishGiveRiceVo =
                userPublishGiveRiceService.querySuitInfo(conditions);
        if (null != userPublishGiveRiceVo) {
            UserAcceptRiceRecordPut recordPut = new UserAcceptRiceRecordPut();
            recordPut.setAcceptRiceId(acceptRiceId);
            recordPut.setGiveRiceId(userPublishGiveRiceVo.getId());
            recordPut.setAcceptUserId(userAccount);
            userAcceptRiceRecordService.save(recordPut);

            //修改出饭状态
            UserPublishGiveRicePut giveRicePut = new UserPublishGiveRicePut();
            giveRicePut.setId(userPublishGiveRiceVo.getId());
            giveRicePut.setBizStatus(1);
            userPublishGiveRiceService.update(giveRicePut);

            return true;
        }

        return false;
    }

}
